/**
 * 波次调试工具
 * 提供开发期间的调试功能
 */
export default class WaveDebugger {
  constructor(waveManager, characterManager) {
    this.waveManager = waveManager;
    this.characterManager = characterManager;
    this.debugMode = false;
    this.keysPressed = {};
    
    // 绑定调试快捷键
    window.addEventListener('keydown', (e) => this.handleKeyDown(e));
    window.addEventListener('keyup', (e) => this.handleKeyUp(e));
  }

  handleKeyDown(e) {
    this.keysPressed[e.key] = true;
    
    // Ctrl+Shift+D 切换调试模式
    if (this.keysPressed['Control'] && this.keysPressed['Shift'] && e.key === 'd') {
      this.debugMode = !this.debugMode;
      console.log(`调试模式 ${this.debugMode ? '开启' : '关闭'}`);
    }
    
    // 数字键1-9: 跳转到指定波次
    if (this.debugMode && e.key >= '1' && e.key <= '9') {
      const targetWave = parseInt(e.key);
      this.waveManager.wave = targetWave - 1;
      this.waveManager.startNextWave();
      console.log(`跳转到第 ${targetWave} 波`);
    }
    
    // F1: 强制生成敌人
    if (this.debugMode && e.key === 'F1') {
      const enemy = this.waveManager.spawnEnemy();
      console.log('强制生成敌人:', enemy);
    }
    
    // F2: 清除所有敌人
    if (this.debugMode && e.key === 'F2') {
      const enemies = this.characterManager.getEnemies();
      enemies.forEach(e => this.characterManager.removeCharacter(e));
      console.log(`已清除 ${enemies.length} 个敌人`);
    }
  }

  handleKeyUp(e) {
    this.keysPressed[e.key] = false;
  }

  renderDebugInfo(ctx) {
    if (!this.debugMode) return;
    
    ctx.fillStyle = 'rgba(0,0,0,0.7)';
    ctx.fillRect(10, 10, 250, 150);
    
    ctx.fillStyle = '#00FF00';
    ctx.font = '14px Arial';
    ctx.textAlign = 'left';
    
    const waveInfo = this.waveManager.getWaveInfo();
    const enemies = this.characterManager.getEnemies();
    const aliveEnemies = enemies.filter(e => !e.isDying);
    
    let y = 30;
    ctx.fillText(`=== 调试模式 ===`, 20, y);
    y += 20;
    ctx.fillText(`波次: ${waveInfo.wave}`, 20, y);
    y += 20;
    ctx.fillText(`敌人: ${aliveEnemies.length}/${waveInfo.enemiesPerWave}`, 20, y);
    y += 20;
    ctx.fillText(`状态: ${waveInfo.isSpawning ? '生成中' : '等待'}`, 20, y);
    y += 20;
    ctx.fillText(`总角色: ${this.characterManager.characters.length}`, 20, y);
    y += 20;
    ctx.fillText(`死亡中: ${enemies.length - aliveEnemies.length}`, 20, y);
    y += 20;
    ctx.fillText(`快捷键: 1-9跳波 F1生成 F2清除`, 20, y);
  }
}